clear
clear all
cap log close

log using "log-files/2_Figure_3.smcl", replace smcl

timer on 2

*----------*
* Figure 3 *
*----------*
	* Figure 3a
	*----------
		* Loading our computo data, with the numeric date. 
		* Then we create the string date from the numeric date                 
		import excel using "data/inputs/1_Bolivia_data_computo.xlsx", ///
			clear first sheet("2.RecepcionSobres")

		keep if ElecActa == "Presidente y Vicepresidente"

		keep Pais - NumMesa CC - Nulos ComputoDate
		drop ValidosenActa

		gen hour = hh(ComputoDate)
		gen minute = mm(ComputoDate)
		gen second = ss(ComputoDate)
		replace second = floor(second)

		gen date = dofc(ComputoDate)
		format date %td
		gen day = day(date)
		gen month = month(date)
		gen year = year(date)

		replace minute = minute + 1 if second > 30
		replace hour = hour + 1 if minute == 60
		replace minute = 0 if minute == 60

		gen ampm = ""
		replace ampm = "a.m." if hour < 12
		replace ampm = "p.m." if hour >= 12
		replace hour = 12 if hour == 0
		replace hour = hour - 12 if ampm == "p.m." & hour != 12

		tostring hour minute second day month year, replace
		replace minute = "0" + minute if length(minute) == 1
		replace hour = "0" + hour if length(hour) == 1

		gen ComputoDate_string = day + "/" + month + "/" + year + " " + ///
			hour + ":" + minute + " " + ampm
		drop hour minute second date day month year ampm

		foreach x of varlist CC-Nulos {
			destring `x', force replace
			replace `x' = 0 if `x' == .
		}

		* Generating the MAS vote share and the pcs variable WITH THE STRING DATE
		* Measures that DO USE Blancos and Nulos (sorted)
		gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + ///
			Blancos + Nulos
		*---*
		gen mshare_so = MAS / total_so
		gen cshare_so = CC / total_so
		gen x = runiform()
		sort ComputoDate_string x
		gen cumsumtotal_so = sum(total_so)
		*---*
		egen maxcumsumtotal_so = max(cumsumtotal_so)
		gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
		drop cumsumtotal_so maxcumsumtotal_so total_so

		replace mshare_so = mshare_so * 100

		twoway (lowess mshare_so pcs_so if pcs_so < 0.95, lcolor(midblue) ///
				lwidth(thick) sort mean bwidth(0.3)) ///
			(lowess mshare_so pcs_so if pcs_so >= 0.95, lcolor(midblue) ///
				lwidth(thick) sort mean bwidth(0.6)), ///
			ylabel(0(0.20)1) xline(0.95, lcolor(black)) yline(50, ///
				lcolor(black)) xlabel(0.95) ///
			graphregion(color(white)) legend(off) ///
			ylabel(0 20 40 60 80 100, angle(0)) ///
			xtitle("Proportion of Votes Counted in Definitive Results System") ///
			ytitle("Polling Station Level MAS Vote Share")
		graph export "outputs/generated/Figure_3a.pdf", replace

	* Figure 3b
	*----------
		* Now we regenerate the % counted using the correct chronological date
		drop pcs_so ComputoDate_string mshare_so cshare_so x //Just to avoid mistakes

		* Measures that DO USE Blancos and Nulos (sorted)
		*---*
		gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + ///
			Blancos + Nulos
		*---*
		gen mshare_so = MAS / total_so
		gen cshare_so = CC / total_so
		gen x = runiform()
		sort ComputoDate x
		gen cumsumtotal_so = sum(total_so)
		*---*
		egen maxcumsumtotal_so = max(cumsumtotal_so)
		gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
		drop cumsumtotal_so maxcumsumtotal_so total_so

		replace mshare_so = mshare_so * 100

		twoway (lowess mshare_so pcs_so if pcs_so < 0.95, lcolor(midblue) ///
				lwidth(thick) sort mean bwidth(0.3)) ///
			(lowess mshare_so pcs_so if pcs_so >= 0.95, lcolor(midblue) ///
				lwidth(thick) sort mean bwidth(0.6)), ///
			ylabel(0(0.20)1) xline(0.95, lcolor(black)) yline(50, ///
				lcolor(black)) xlabel(0.95) ///
			graphregion(color(white)) legend(off) ///
			ylabel(0 20 40 60 80 100, angle(0)) ///
			xtitle("Proportion of Votes Counted in Definitive Results System") ///
			ytitle("Polling Station Level MAS Vote Share")
		graph export "outputs/generated/Figure_3b.pdf", replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
